package cn.wing.utils;


import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.world.item.ItemStack;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class TransactionInfo {

    private String uuid;

    public TransactionInfo(UUID player, ItemStack sellItemStack, List<ItemStack> buyItemStack, String notes) {
        this.player = player;
        this.sellItemStack = sellItemStack;
        this.buyItemStack = buyItemStack;
        this.notes = notes;
        uuid = UUID.randomUUID().toString()+System.currentTimeMillis();
    }

    private UUID player;

    private ItemStack sellItemStack;

    private List<ItemStack> buyItemStack;

    private String notes;


    public UUID getPlayer() {
        return player;
    }

    public void setPlayer(UUID player) {
        this.player = player;
    }

    public ItemStack getSellItemStack() {
        return sellItemStack;
    }

    public void setSellItemStack(ItemStack sellItemStack) {
        this.sellItemStack = sellItemStack;
    }

    public List<ItemStack> getBuyItemStack() {
        return buyItemStack;
    }

    public void setBuyItemStack(List<ItemStack> buyItemStack) {
        this.buyItemStack = buyItemStack;
    }

    public String getNotes() {
        return notes;
    }

    public void setNotes(String notes) {
        this.notes = notes;
    }

    public String getUuid() {
        return uuid;
    }

    public void setUuid(String uuid) {
        this.uuid = uuid;
    }

    /* aaa → CompoundTag */
    public static CompoundTag save(TransactionInfo obj) {
        CompoundTag tag = new CompoundTag();
        tag.put("icon", obj.getSellItemStack().save(new CompoundTag()));

        ListTag listTag = new ListTag();
        for (ItemStack s : obj.getBuyItemStack()) {
            listTag.add(s.save(new CompoundTag()));
        }
        tag.put("list", listTag);
        tag.putUUID("str1", obj.getPlayer());
        tag.putString("str2", obj.getNotes());
        tag.putString("str3", obj.getUuid());
        return tag;
    }

    /* CompoundTag → aaa */
    public static TransactionInfo load(CompoundTag tag) {
        ListTag listTag = tag.getList("list", Tag.TAG_COMPOUND);
        List<ItemStack> list = new ArrayList<>();
        for (int i = 0; i < listTag.size(); i++) {
            list.add(ItemStack.of(listTag.getCompound(i)));
        }
        TransactionInfo obj = new TransactionInfo(tag.getUUID("str1"), ItemStack.of(tag.getCompound("icon")), list, tag.getString("str2"));
        obj.setUuid(tag.getString("str3"));
        return obj;
    }

}
